﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using YDModel;
using YDServices.Comm;

namespace YDServices
{
    public class SpecService : BaseService
    {
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="conds"></param>
        /// <returns></returns>
        public List<T_Spec> getList(ConditionItem[] conds)
        { 
             try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var list = db.T_Spec.Where(o=>o.State!=false).AsQueryable();
                    if (conds != null)
                    {
                        var qb = QueryBuilder<T_Spec>.Create().AddConditions(conds);
                        list = list.Where(qb.ToExpression());
                    }
                    return list.ToList();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }

        /// <summary>
        /// 下拉框
        /// </summary>
        /// <returns></returns>
        public List<T_Spec_List> getComboxList(long factoryID)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    List<T_Spec> specList= new List<T_Spec>(); 
                    if (factoryID == 0)
                    {
                        specList = db.T_Spec.Where(o => o.Factory_ID == factoryID && o.State != false).ToList();
                    }
                    else
                    {
                        specList= db.T_Spec.Where(o => (o.Factory_ID == 0 || o.Factory_ID== factoryID) && o.State != false).ToList();
                    }

                    List<T_Spec_List> list = specList.Where(o => o.State != false ) 
                        .Select(o => new T_Spec_List { ID = o.ID, SpecName = o.SpecName }).ToList();
   

                    return list;
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
                return null;
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="tu"></param>
        public void AddItem(T_Spec tu)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var user = db.T_Spec.Where(o => o.SpecName == tu.SpecName && o.State == true).FirstOrDefault();
                    if (user != null)
                    {
                        throw new ApplicationException("规格已存在!");
                    }

                    tu.State = true;
                    tu.CreatedTime = GetSqlNow();
                    tu.CreatedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                    db.T_Spec.Add(tu);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="tu"></param>
        public void EditItem(T_Spec tu)
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    var item = db.T_Spec.Where(o => o.ID == tu.ID && o.State == true).FirstOrDefault();
                    if (item == null)
                    {
                        throw new ApplicationException("规格信息不存在!");
                    }
                    item.Factory_ID = tu.Factory_ID;
                    item.Factory_Name = tu.Factory_Name;
                    item.SpecName = tu.SpecName;
                  //  item.SpecType = tu.SpecType;
                    item.ModifiedTime = GetSqlNow();
                    item.ModifiedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="tu"></param>
        /// <param name="loginUser"></param>
        public void DelItem(T_Spec item)  //List<T_Spec> tuList 
        {
            try
            {
                using (YDDataEntities db = new YDDataEntities())
                {
                    
                    var fact = db.T_Spec.Where(o => o.ID == item.ID ).FirstOrDefault();
                    if (fact == null)
                    {
                        throw new ApplicationException("规格信息不存在!");
                    }
                    fact.State = false;
                    fact.DeletedTime = GetSqlNow();
                    fact.DeletedBy = "[" + Global.loginUser.ID + "]" + Global.loginUser.UserName;
                    
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ex);
                WriteException(ex);
            }
        }

        

    }
}
